package com.ruoyi.web.controller.sale;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.sale.domain.SaleOrder;
import com.ruoyi.sale.service.ISaleOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 销售订单Controller
 * 
 * @author ruoyi
 * @date 2024-10-28
 */
@RestController
@Anonymous
@RequestMapping("/sale/order")
public class SaleOrderController extends BaseController
{
    @Autowired
    private ISaleOrderService saleOrderService;


    /**
     * 生成订单号
     * @return
     */
    @GetMapping("/getXSDD")
    public AjaxResult getXSDD( ){
        return success(SeqNumberUtils.getXSDDNumber());
    }

    /**
     * 查询销售订单列表
     */
    @PreAuthorize("@ss.hasPermi('sale:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SaleOrder saleOrder)
    {
        startPage();
        List<SaleOrder> list = saleOrderService.selectSaleOrderList(saleOrder);
        return getDataTable(list);
    }

    /**
     * 导出销售订单列表
     */
    @PreAuthorize("@ss.hasPermi('sale:order:export')")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleOrder saleOrder)
    {
        List<SaleOrder> list = saleOrderService.selectSaleOrderList(saleOrder);
        ExcelUtil<SaleOrder> util = new ExcelUtil<SaleOrder>(SaleOrder.class);
        util.exportExcel(response, list, "销售订单数据");
    }

    /**
     * 获取销售订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('sale:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(saleOrderService.selectSaleOrderById(id));
    }

    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody SaleOrder saleOrder){
        return success(saleOrderService.updateStatusSaleOrder( saleOrder));
    }

    /**
     * 新增销售订单
     */
    @PreAuthorize("@ss.hasPermi('sale:order:add')")
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SaleOrder saleOrder)
    {
        Long userId = getUserId();
        saleOrder.setCreateBy(userId.toString());
        saleOrder.setSaleUserId(userId);
        return toAjax(saleOrderService.insertSaleOrder(saleOrder));
    }

    /**
     * 修改销售订单
     */
    @PreAuthorize("@ss.hasPermi('sale:order:edit')")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SaleOrder saleOrder)
    {
        Long userId = getUserId();
        saleOrder.setUpdateBy(String.valueOf(userId));
        return toAjax(saleOrderService.updateSaleOrder(saleOrder));
    }

    /**
     * 删除销售订单
     */
    @PreAuthorize("@ss.hasPermi('sale:order:remove')")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(saleOrderService.deleteSaleOrderByIds(ids));
    }
}
